package com.jxw.update;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.jxw.launcher.MyApp;
import com.jxw.launcher.R;
import com.jxw.launcher.util.CheckFileAndDown;
import com.jxw.launcher.util.Logger;
import com.jxw.launcher.util.Util;
import com.jxw.utils.SharedPreferencesUtil;
import com.jxw.utils.ToastUtil;
import com.zzj.filedownloader.download.DownLoadListener;
import com.zzj.filedownloader.download.DownLoadManager;
import com.zzj.filedownloader.download.TaskInfo;
import com.zzj.filedownloader.download.dbcontrol.bean.SQLDownLoadInfo;
import java.io.File;
import java.io.IOException;
import net.lingala.zip4j.util.InternalZipConstants;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class Fragment3 extends Fragment implements View.OnClickListener {
    private static final String CHECK_URL = Util.API_HOST + "/api/jxwota/ansystem/check?apkIsbn=zndzgkptv5&version=5.0";
    boolean cancheck;
    private DownLoadManager downLoadManager;
    Button go_down;
    public boolean isDownload;
    private UpdateActivity mAct;
    private View mView;
    private AlertDialog netDialog;
    ProgressBar pb_progressbar;
    private int position;
    private View pro;
    TextView result;
    private int size;
    Button start_check;
    Button stop_check;
    TextView text_count;
    TextView text_pro;
    Thread thread;
    TextView tip;
    TextView tips;
    Handler mHandler = new Handler() { // from class: com.jxw.update.Fragment3.1
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (message.what == 0) {
                int i = message.arg1;
                int i2 = (i * 100) / Fragment3.this.len;
                TextView textView = Fragment3.this.text_pro;
                textView.setText(i2 + "%");
                TextView textView2 = Fragment3.this.text_count;
                textView2.setText(i + InternalZipConstants.ZIP_FILE_SEPARATOR + Fragment3.this.len);
                Fragment3.this.pb_progressbar.setProgress(i2);
            } else if (message.what == 1) {
                Fragment3.this.pro.setVisibility(8);
                Fragment3.this.tips.setVisibility(8);
                Fragment3.this.stop_check.setVisibility(8);
                if (Fragment3.this.mAct.paths.size() == 0) {
                    if (Fragment3.this.isAdded()) {
                        Fragment3.this.showDialog();
                    }
                    Fragment3.this.tip.setVisibility(8);
                    Fragment3.this.start_check.setVisibility(0);
                    Fragment3.this.go_down.setVisibility(8);
                    return;
                }
                Fragment3.this.tip.setVisibility(0);
                Fragment3.this.result.setText(message.obj.toString());
                Fragment3.this.go_down.setVisibility(0);
            } else if (message.what == 2) {
                Fragment3.this.pro.setVisibility(8);
                Fragment3.this.tips.setVisibility(8);
                Fragment3.this.stop_check.setVisibility(8);
                Fragment3.this.tip.setVisibility(8);
                Fragment3.this.start_check.setVisibility(0);
                Fragment3.this.go_down.setVisibility(8);
                ToastUtil.showTextToastDia(Fragment3.this.getContext(), "校验失败，请检查网络!");
            }
        }
    };
    int len = 1;

    static /* synthetic */ int access$708(Fragment3 fragment3) {
        int i = fragment3.position;
        fragment3.position = i + 1;
        return i;
    }

    private void showDialog_sure() {
        final AlertDialog create = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_update_sure, (ViewGroup) null);
        ((TextView) inflate.findViewById(R.id.cancel_btn)).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment3.2
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                create.cancel();
                create.dismiss();
            }
        });
        create.setView(inflate);
        create.setCancelable(false);
        create.setCanceledOnTouchOutside(false);
        create.show();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void showDialog() {
        final AlertDialog create = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_update, (ViewGroup) null);
        ((TextView) inflate.findViewById(R.id.cancel_btn)).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment3.3
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                create.cancel();
                create.dismiss();
            }
        });
        create.setView(inflate);
        create.setCancelable(false);
        create.setCanceledOnTouchOutside(false);
        create.show();
    }

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        this.mView = layoutInflater.inflate(R.layout.fragment_2, viewGroup, false);
        this.pb_progressbar = (ProgressBar) this.mView.findViewById(R.id.pb_progressbar);
        this.start_check = (Button) this.mView.findViewById(R.id.start_check);
        this.stop_check = (Button) this.mView.findViewById(R.id.stop_check);
        this.go_down = (Button) this.mView.findViewById(R.id.go_down);
        this.text_pro = (TextView) this.mView.findViewById(R.id.text_pro);
        this.text_count = (TextView) this.mView.findViewById(R.id.text_count);
        this.tip = (TextView) this.mView.findViewById(R.id.tip);
        this.tips = (TextView) this.mView.findViewById(R.id.tips);
        this.isDownload = false;
        this.mAct = (UpdateActivity) getActivity();
        this.cancheck = true;
        this.downLoadManager = new DownLoadManager(MyApp.getInstance());
        this.downLoadManager.setAllTaskListener(new DownloadManagerListener());
        this.go_down.setVisibility(8);
        this.result = (TextView) this.mView.findViewById(R.id.result);
        this.pro = this.mView.findViewById(R.id.pro);
        this.start_check.setOnClickListener(this);
        this.stop_check.setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment3.4
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment3.this.showExitDialog();
            }
        });
        this.go_down.setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment3.5
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Log.i("sadf", "onClick: ::" + Fragment3.this.isDownload);
                ((UpdateActivity) Fragment3.this.getActivity()).goDown();
            }
        });
        return this.mView;
    }

    public void exit() {
        Thread thread = this.thread;
        if (thread != null) {
            thread.interrupt();
        }
        this.cancheck = false;
    }

    @Override // android.support.v4.app.Fragment
    public void onHiddenChanged(boolean z) {
        super.onHiddenChanged(z);
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        if (CheckFileAndDown.isAllDownSus) {
            this.pro.setVisibility(0);
            this.start_check.setVisibility(8);
            this.stop_check.setVisibility(0);
            this.cancheck = true;
            this.tips.setVisibility(0);
            this.thread = new Thread(new Runnable() { // from class: com.jxw.update.Fragment3.6
                @Override // java.lang.Runnable
                public void run() {
                    Fragment3.this.checkFiles();
                }
            });
            this.thread.start();
            return;
        }
        ToastUtil.showTextToastDia(MyApp.getInstance(), "正在下载文件,请稍等...");
    }

    public void checkFiles() {
        new OkHttpClient().newCall(new Request.Builder().url(CHECK_URL).build()).enqueue(new Callback() { // from class: com.jxw.update.Fragment3.7
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                Log.d("zszj", "onFailure=" + iOException.getMessage());
                Message obtainMessage = Fragment3.this.mHandler.obtainMessage();
                obtainMessage.what = 2;
                Fragment3.this.mHandler.sendMessage(obtainMessage);
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("zszj", "onResponse=" + response.toString());
                String string = response.body().string();
                SharedPreferencesUtil.putSpString(Fragment3.this.getContext(), CheckFileAndDown.SP_FILE_ARRAY, string);
                Fragment3.this.mHandler.sendEmptyMessage(0);
                Fragment3.this.mAct.paths.clear();
                StringBuilder sb = new StringBuilder();
                try {
                    JSONArray jSONArray = new JSONObject(string).getJSONArray("data");
                    Log.i("zszj", "JSONArray: " + jSONArray.toString());
                    Fragment3.this.mAct.paths = CheckFileAndDown.checkFile1(Fragment3.this.getContext(), jSONArray);
                    Fragment3.this.size = Fragment3.this.mAct.paths.size();
                    for (int i = 0; i < Fragment3.this.size; i++) {
                        sb.append(Fragment3.this.mAct.paths.get(i) + " 文件缺少\n");
                    }
                    Log.d("zszj", "onResponse=try" + sb.toString());
                } catch (JSONException e) {
                    Log.d("zszj", "onResponse=catch");
                    e.printStackTrace();
                }
                Message obtainMessage = Fragment3.this.mHandler.obtainMessage();
                obtainMessage.what = 1;
                obtainMessage.obj = sb;
                Fragment3.this.mHandler.sendMessage(obtainMessage);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void downPoi(int i) {
        if (this.size > i) {
            this.isDownload = true;
            CheckFileAndDown.downFile(this.mAct.paths, i, this.downLoadManager);
            Log.e("gwj", "下载");
        }
    }

    public void downFile(String str, String str2) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + str);
        try {
            if (file.exists()) {
                Logger logger = Logger.getLogger();
                logger.d("deleteTask=" + file.getAbsolutePath());
                file.delete();
            }
            File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + str + ".tmp");
            if (file2.exists()) {
                Logger logger2 = Logger.getLogger();
                logger2.d("deleteTask=" + file2.getAbsolutePath());
                file2.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.downLoadManager.setSupportBreakpoint(true);
        this.downLoadManager.addTask(str, str2, file.getName(), file.getAbsolutePath());
        this.downLoadManager.startTask(str);
    }

    /* loaded from: classes.dex */
    private class DownloadManagerListener implements DownLoadListener {
        private DownloadManagerListener() {
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onStart(SQLDownLoadInfo sQLDownLoadInfo) {
            Logger logger = Logger.getLogger();
            logger.d("onStart,name=" + sQLDownLoadInfo.getTaskID());
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onProgress(SQLDownLoadInfo sQLDownLoadInfo, boolean z) {
            TaskInfo taskInfo = Fragment3.this.downLoadManager.getTaskInfo(sQLDownLoadInfo.getTaskID());
            if (taskInfo != null) {
                taskInfo.setDownFileSize(sQLDownLoadInfo.getDownloadSize());
                taskInfo.setFileSize(sQLDownLoadInfo.getFileSize());
                taskInfo.setOnDownloading(true);
                Fragment3.this.pb_progressbar.setProgress(taskInfo.getProgress());
                TextView textView = Fragment3.this.text_pro;
                textView.setText(taskInfo.getProgress() + "%");
            }
            Logger logger = Logger.getLogger();
            logger.d("onProgress,name=" + sQLDownLoadInfo.getTaskID() + ",size=" + sQLDownLoadInfo.getDownloadSize());
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onStop(SQLDownLoadInfo sQLDownLoadInfo, boolean z) {
            Logger logger = Logger.getLogger();
            logger.d("onStop,name=" + sQLDownLoadInfo.getTaskID());
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onSuccess(SQLDownLoadInfo sQLDownLoadInfo) {
            Logger logger = Logger.getLogger();
            logger.d("onSuccess,name=" + sQLDownLoadInfo.getTaskID() + ",position=" + Fragment3.this.position);
            Fragment3.access$708(Fragment3.this);
            TextView textView = Fragment3.this.text_count;
            textView.setText(Fragment3.this.position + InternalZipConstants.ZIP_FILE_SEPARATOR + Fragment3.this.size);
            if (Fragment3.this.position < Fragment3.this.size) {
                Fragment3 fragment3 = Fragment3.this;
                fragment3.downPoi(fragment3.position);
                return;
            }
            Fragment3 fragment32 = Fragment3.this;
            fragment32.isDownload = false;
            fragment32.cancheck = false;
            fragment32.stop_check.setVisibility(8);
            Fragment3.this.start_check.setVisibility(0);
            Fragment3.this.go_down.setVisibility(8);
            Fragment3.this.pro.setVisibility(8);
            Fragment3.this.tips.setVisibility(8);
            CheckFileAndDown.isAllDownSus = true;
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onError(SQLDownLoadInfo sQLDownLoadInfo) {
            Logger logger = Logger.getLogger();
            logger.d("onError,name=" + sQLDownLoadInfo.getTaskID());
            Fragment3.this.downLoadManager.deleteTask(sQLDownLoadInfo.getTaskID());
            Fragment3.access$708(Fragment3.this);
            TextView textView = Fragment3.this.text_count;
            textView.setText(Fragment3.this.position + InternalZipConstants.ZIP_FILE_SEPARATOR + Fragment3.this.size);
            if (Fragment3.this.position < Fragment3.this.size) {
                Fragment3 fragment3 = Fragment3.this;
                fragment3.downPoi(fragment3.position);
                return;
            }
            Fragment3 fragment32 = Fragment3.this;
            fragment32.isDownload = false;
            fragment32.cancheck = false;
            fragment32.stop_check.setVisibility(8);
            Fragment3.this.start_check.setVisibility(0);
            Fragment3.this.go_down.setVisibility(8);
            Fragment3.this.pro.setVisibility(8);
            Fragment3.this.tips.setVisibility(8);
            CheckFileAndDown.isAllDownSus = true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void showExitDialog() {
        AlertDialog alertDialog = this.netDialog;
        if (alertDialog != null) {
            alertDialog.show();
            return;
        }
        this.netDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.dialog_exit, (ViewGroup) null);
        inflate.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment3.8
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment3.this.netDialog.cancel();
            }
        });
        inflate.findViewById(R.id.set).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment3.9
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                if (Fragment3.this.thread != null) {
                    Fragment3.this.thread.interrupt();
                }
                Fragment3 fragment3 = Fragment3.this;
                fragment3.cancheck = false;
                fragment3.stop_check.setVisibility(8);
                Fragment3.this.start_check.setVisibility(0);
                Fragment3.this.pro.setVisibility(8);
                Fragment3.this.tips.setVisibility(8);
                Fragment3.this.netDialog.cancel();
            }
        });
        this.netDialog.setView(inflate);
        this.netDialog.setCancelable(false);
        this.netDialog.setCanceledOnTouchOutside(false);
        this.netDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { // from class: com.jxw.update.Fragment3.10
            @Override // android.content.DialogInterface.OnKeyListener
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return i == 84;
            }
        });
        this.netDialog.show();
    }
}
